﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CosyTrade.Organization;
using CosyTrade.UI;
using CosyTrade.Security;

namespace CosyTrade.ClientForm.Organization
{
    public partial class EditPassword : FormBase
    {
        public EditPassword()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            UpdatePwd();
        }

        void UpdatePwd()
        {
            if (GlobalInfo.CurrentEmployee == null)
            {
                Functions.ShowMessageBox("未指定员工");
                return;
            }

            if (textOld.EditValue==null || string.IsNullOrEmpty(textOld.EditValue.ToString()))
            {
                Functions.ShowMessageBox("请输入原始密码");
                return;
            }
            if (!Role.Encode(textOld.EditValue.ToString()).Equals(GlobalInfo.CurrentEmployee.MaptoRole.Password))
            {
                Functions.ShowMessageBox("原始密码错误");
                return;
            }

            if (textNew.EditValue == null || string.IsNullOrEmpty(textNew.EditValue.ToString()))
            {
                Functions.ShowMessageBox("请输入新密码");
                return;
            }
            if (textCom.EditValue == null || string.IsNullOrEmpty(textCom.EditValue.ToString()))
            {
                Functions.ShowMessageBox("请输入确认密码");
                return;
            }
            if ((textNew.EditValue != null && textCom.EditValue != null) && !textNew.EditValue.ToString().Equals(textCom.EditValue.ToString()))
            {
                Functions.ShowMessageBox("新密码与确认不一致");
                return;
            }

            GlobalInfo.CurrentEmployee.MaptoRole.Password = Role.Encode(textNew.EditValue.ToString());
            Operation.SecuritySystem.UpdateRole(GlobalInfo.CurrentEmployee.MaptoRole);

            DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}